<template>
  <div class="mod-subscribeindex">
    <el-form
      ref="searchForm"
      :inline="true"
      :rules="dataRule"
      :model="searchForm"
      @keyup.enter.native="getDataList()" >
      <el-form-item prop="unitBuilding">
        <el-input v-model="searchForm.roomNumber" placeholder="房间号" clearable></el-input>
      </el-form-item>
      <el-form-item prop="unitBuilding">
        <el-input v-model="searchForm.unitBuilding" placeholder="楼栋单元" clearable></el-input>
      </el-form-item>
      <el-form-item prop="floor">
        <el-input v-model="searchForm.floor" placeholder="楼层" clearable></el-input>
      </el-form-item>
      <el-form-item prop="value1">
          <el-date-picker
      v-model="searchForm.value1"
      type="daterange"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期">
    </el-date-picker>
      </el-form-item>
      <el-form-item prop="startTime">
        <el-time-select
          placeholder="起始时间"
          v-model="searchForm.startTime"
          :picker-options="{
            start: '08:30',
            end: '18:30'
          }">
        </el-time-select>
      </el-form-item>
      <el-form-item prop="endTime">
        <el-time-select
          placeholder="结束时间"
          v-model="searchForm.endTime"
          :picker-options="{
            start: '08:30',
            end: '18:30',
            minTime: searchForm.startTime
          }"
        ></el-time-select>
      </el-form-item>
      <el-form-item prop="personNumber">
        <el-input v-model="searchForm.personNumber" placeholder="容纳人数" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column prop="roomName" header-align="center" align="center" label="会议室名称"></el-table-column>
      <el-table-column prop="roomNumber" header-align="center" align="center" label="房间号"></el-table-column>
      <el-table-column prop="unitBuilding" header-align="center" align="center" label="楼栋单元"></el-table-column>
      <el-table-column prop="floor" header-align="center" align="center" label="楼层"></el-table-column>
      <el-table-column prop="personNumber" header-align="center" align="center" label="容纳人数"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="subscribeHandle(scope.row)">预约</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchForm: {
        unitBuilding: '',
        floor: '',
        startTime: '',
        endTime: '',
        roomNumber: '',
        value1: []
      },
      dataRule: {
        value1: [{ required: true, message: '请选择预约日期', trigger: 'blur' }],
        startTime: [{ required: true, message: '请选择预约开始时间', trigger: 'blur' }],
        endTime: [{ required: true, message: '请选择预约结束时间', trigger: 'blur' }]
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      startTime: '',
      endTime: ''
    }
  },
  activated () {},
  methods: {
    // 获取数据列表
    getDataList () {
      this.$refs['searchForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: '/base/meetingroominfo/getAvailableRoom',
            method: 'post',
            data: {
              page: this.pageIndex,
              limit: this.pageSize,
              startDate: this.dateFtt('yyyy-MM-dd', this.searchForm.value1[0]),
              endDate: this.dateFtt('yyyy-MM-dd', this.searchForm.value1[1]),
              startTime: this.searchForm.startTime + ':00',
              endTime: this.searchForm.endTime + ':00',
              unitBuilding: this.searchForm.unitBuilding,
              floor: this.searchForm.floor,
              roomNumber: this.searchForm.roomNumber,
              personNumber: this.searchForm.personNumber
            }
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataList = data.page.records
              this.totalPage = data.page.total
            } else {
              this.dataList = []
              this.totalPage = 0
            }
          })
        } else {
          return false
        }
      })
    },
    dateFtt (fmt, date) {
      var o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        S: date.getMilliseconds() // 毫秒
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
      }
      for (var k in o) {
        // eslint-disable-next-line eqeqeq
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length === 1
              ? o[k]
              : ('00' + o[k]).substr(('' + o[k]).length)
          )
        }
      }
      return fmt
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 预约
    subscribeHandle (row) {
      this.$confirm('确认预约?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/subscribe/detailinfo/subscribingRoom',
          method: 'post',
          data: {
            roomId: row.id,
            startDate: this.dateFtt('yyyy-MM-dd', this.searchForm.value1[0]),
            endDate: this.dateFtt('yyyy-MM-dd', this.searchForm.value1[1]),
            startTime: this.searchForm.startTime + ':00',
            endTime: this.searchForm.endTime + ':00'
          }
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              type: 'success',
              message: '预约成功'
            })
            this.getDataList()
          }
        })
      }).catch(() => {

      })
    }
  }
}
</script>
